import random
import re

from unit_tools.handle_data.yaml_handler import get_extract_yaml


class DebugTalk:

    def get_extratc_data(self, node_name, out_format=None):
        """
        获取extract.yaml数据，首先判断out_format是否为数字类型，如果不是就获取下一个节点的value
        :param node_name: extract.yaml文件中的key
        :param out_format: str类型，0：随机读取；-1：读取全部数据，返回字符串格式；-2：读取全部，返回列表格式
        :return:
        """
        data = get_extract_yaml(node_name)
        if out_format is not None and bool(re.compile(r"^[+-]?\d+$").match(str(out_format))):
            out_format = int(out_format)
            data_value = {
                out_format: self.seq_read(data,out_format),
                0: random.choice(data),
                -1: ",".join(data),
                -2: ",".join(data).split(",")
            }
            data = data_value[out_format]
        else:
            data = get_extract_yaml(node_name,out_format)
        return data

    def seq_read(self, data, out_format):
        """
        获取extract.yaml,读取列表中的某个值
        :param data:
        :param randoms:
        :return:
        """
        if out_format not in [0, -1, -2]:
            return data[out_format - 1]
        else:
            return None

    def params_md5(self):
        pass

    def get_now_time(self):
        return "999999"

if __name__ == '__main__':
    debug = DebugTalk()
    res = debug.get_extratc_data("goodsId","-2")
    print(res)